import React from "react";
// 引入倒计时组件
import CountDown from "@/components/common/countdown";
const Email = () => {

    // 邮箱认证
    const onFinish = async (values: any) => 
    {
        let result = await React.HTTP.post('/business/email', {
            busid: React.Business.id,
            code: values.code,
        });
        if(result.code == 1)
        {
            React.success(result.msg, () => React.navigate('/business/index'));
        }
        else
        {
            React.error(result.msg);
        }
    }

    // 发送邮件
    const SendEmail = async() =>
    {

        let result = await React.HTTP.post('/business/send', {
            busid: React.Business.id
        });
        if(result.code == 1)
        {
            React.success(result.msg, () => {});
        }
        else
        {
            React.error(result.msg);
        }
    }
    return (
        <>
            <React.UI.Form
                layout='horizontal'
                onFinish={onFinish}
                initialValues={{
                    email: React.Business.email,
                    code: ''
                }}
                footer={
                    <React.UI.Button block shape='rounded' type='submit' color='primary' size='large'>
                        提交修改
                    </React.UI.Button>
                }
            >
                <React.UI.Form.Item
                    name='email'
                    label='邮箱'
                    rules={[
                        { required: true },
                        { type: 'string', min: 6 },
                        { type: 'email', warningOnly: true },
                    ]}
                >
                    <React.UI.Input placeholder='请输入邮箱' readOnly/>
                </React.UI.Form.Item>
                <React.UI.Form.Item
                    label='邮箱验证码'
                    name='code'
                    rules={[{ required: true, message: '邮箱验证码不能为空' }]}
                    extra={
                        <CountDown
                            initialSeconds={10}
                            buttonTextBefore="发送验证码"
                            SendEmail={SendEmail}
                        />
                    }
                >
                    <React.UI.Input placeholder='请输入验证码' clearable />
                </React.UI.Form.Item>
            </React.UI.Form>
        </>
    )
}
export default Email;